using System;
using System.IO;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Data.Core.Plugins;
using Avalonia.Markup.Xaml;
using iOSClub.App.Desktop.Models;
using iOSClub.App.Desktop.ViewModels;
using iOSClub.App.Desktop.Views;
using iOSClub.App.Share;
using Newtonsoft.Json;

namespace iOSClub.App.Desktop;

public partial class App : Application
{
    public override void Initialize()
    {
        
        AvaloniaXamlLoader.Load(this);
    }

    public override void OnFrameworkInitializationCompleted()
    {
        if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
        {
            var user = new SettingModel() { Name = "游客" };
            if (File.Exists("data.json"))
            {
                var s = File.ReadAllText("data.json");
                var data = JsonConvert.DeserializeObject<SettingModel>(s);
                if (data != null) user = data;
            }
            else
            {
                File.Create("data.json");
            }

            BindingPlugins.DataValidators.RemoveAt(0);
            desktop.MainWindow = new MainWindow(user)
            {
                DataContext = new MainWindowViewModel()
            };
        }

        base.OnFrameworkInitializationCompleted();
    }

    private void Exit(object? sender, EventArgs e)
    {
        
    }
}